package org.android.brasil.projetos.oktoberfest2011.view;

import java.util.Random;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.negocio.model.Apresentacao;
import org.android.brasil.projetos.oktoberfest2011.utils.DataUtil;
import org.android.brasil.projetos.oktoberfest2011.view.controller.HomeController;
import org.android.brasil.projetos.oktoberfest2011.view.controller.WidgetController;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

public class OktoberfestWidget4x2View extends AppWidgetProvider {

	public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
	public static String ACTION_OPEN_ACTIVITY = "ActionActivityOpen";

	private static RemoteViews views;

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		int N = appWidgetIds.length;

		// Faz um loop para cada App Widget que pertence a esse provider
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];
			updateWidget(context, appWidgetId);
		}
		WidgetController.agendarAtualizacaoDiaria(context, ACTION_WIDGET_RECEIVER);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);

		// Atualiza a widget clicada com uma nova fortune
		if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
			Bundle extras = intent.getExtras();
			if (extras == null) {
				return;
			}
			// Busca o ID do widget nos extras para atualizar apenas o widget
			// clicado
			int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);

			if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
				return;
			updateWidget(context, widgetId);
		}

	}

	public static void updateWidget(Context context, int appWidgetId) {
		// Intent para setar a action Receiver e um Extra com o ID do widget
		Random rdAle = new Random();
		String desfile = "";
		String aPartirDas = "";
		
		Intent intentUpdate = new Intent(context, OktoberfestWidget4x2View.class);
		intentUpdate.setAction(ACTION_WIDGET_RECEIVER);
		intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

		views = new RemoteViews(context.getPackageName(), R.layout.oktoberfest_widget_4x2_view);

		Bitmap bMap = null;

		int dias = WidgetController.getDiasParaEvento();
		
		//muda as frases do widget

		if(dias <= 0){
			//dias = dias * (-1);
			
			dias = WidgetController.getDiasDeEvento();
			
			if(dias == 0){
				views.setViewVisibility(R.idWidget4x2.img_unidades, View.GONE);
				views.setViewVisibility(R.idWidget4x2.img_dezenas, View.GONE);
				views.setViewVisibility(R.idWidget4x2.tv_dias, View.GONE);
				views.setTextViewText(R.idWidget4x2.tv_faltam, context.getResources().getString(R.string.primeiro_dia));
			}
			else{
				
				views.setTextViewText(R.idWidget4x2.tv_dias, context.getResources().getQuantityString(R.plurals.days, dias, dias));
				views.setTextViewText(R.idWidget4x2.tv_faltam, context.getResources().getString(R.string.alemao_ha));
			}
		}
		/*else */
		
		else{
			views.setTextViewText(R.idWidget4x2.tv_dias, context.getResources().getQuantityString(R.plurals.days, dias, dias));
			views.setTextViewText(R.idWidget4x2.tv_faltam, context.getResources().getQuantityString(R.plurals.missing, dias, dias));
		}

		
		int unidades = dias / 10;
		
		bMap = BitmapFactory.decodeResource(context.getResources(),
				WidgetController.escolherImagemNumeroDia(unidades));
		
		views.setImageViewBitmap(R.idWidget4x2.img_dezenas, bMap);

		int dezenas = dias % 10;
		
		bMap = BitmapFactory.decodeResource(context.getResources(),
				WidgetController.escolherImagemNumeroDia(dezenas));

		views.setImageViewBitmap(R.idWidget4x2.img_unidades, bMap);

		Intent intentOpen = new Intent(context, HomeController.class);
		intentOpen.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		PendingIntent actionPendingIntentOpen = PendingIntent.getActivity(context, appWidgetId,
				intentOpen, PendingIntent.FLAG_UPDATE_CURRENT);

		views.setOnClickPendingIntent(R.idWidget4x2.layout_appwidget, actionPendingIntentOpen);
		
		String[] frases1 =  context.getResources().getStringArray(R.array.frases1);
		String[] frases2 = context.getResources().getStringArray(R.array.frases2); 
		
		int maxI = (frases1.length > frases2.length) ? frases2.length : frases1.length ;
		int intRand = rdAle.nextInt(maxI + 1);
		
		
		if(intRand == maxI ){
			int diasEvento = WidgetController.getDiasDeEvento();
			if( diasEvento > 0 && diasEvento <= 17) {
				desfile = diasEvento + " " + context.getResources().getString(R.string.dias_bebedeira) ;
				aPartirDas = context.getResources().getString(R.string.cotinue_assim);
			} else {
				intRand--;
			}
		} 
		
		if(desfile.equals("") || aPartirDas.equals("")) {
			desfile = frases1[intRand] ;
			aPartirDas = frases2[intRand];
		}
		/* String desfile = context.getResources().getString(R.string.nao_tem_desfile);
		String aPartirDas = context.getResources().getString(R.string.tomar_umas);
		*/
		
		if (WidgetController.temDesfileHoje(context)) {
			desfile = context.getResources().getString(R.string.tem_desfile);
			Apresentacao apr = WidgetController.getDesfileDeHoje(context);
			String inicio = DataUtil.formatarHora(apr.getHoraInicio());
			aPartirDas =  context.getResources().getString(R.string.a_partir) +" "+ inicio;
			
			Intent intentDesfile = new Intent(context, HorariosTabView.class);
			intentDesfile.putExtra("DESFILE", true);
			PendingIntent actionPendingIntentOpenDesfile = PendingIntent.getActivity(context, appWidgetId,
					intentDesfile, PendingIntent.FLAG_UPDATE_CURRENT);

			views.setOnClickPendingIntent(R.idWidget4x2.tv_tem_desfile, actionPendingIntentOpenDesfile);
			views.setOnClickPendingIntent(R.idWidget4x2.tv_a_partir, actionPendingIntentOpenDesfile);

			

		}
		
		views.setTextViewText(R.idWidget4x2.tv_tem_desfile, desfile);
		views.setTextViewText(R.idWidget4x2.tv_a_partir, aPartirDas);

		AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);

	}
}